const request = require('request');
const common = appRequire('controllers/wx/common');

// 导出对象，供其它模块调用
module.exports = {
    wxQrcode,
    wxQrcodeFunc,
};

async function wxQrcode(req, response) {
    let postData = req.body
    const returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, ['page'], response);
    if (checkResult == false) return;

    returnData.data = await wxQrcodeFunc(req)
    response.json(returnData)
}

async function wxQrcodeFunc(req) {
    return new Promise(async function (resolve, reject) {
        let postData = req.body
        let token = await common.getAccessToken(req)

        request.post({
            url: `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=` + token,
            encoding: "base64",
            json: {
                scene: postData.scene,
                page: postData.page
            }
        }, async (err, httpResponse, body) => {
            resolve("data:image/PNG;base64," + body)
        })
    })
}